package com.asm.slidingmenu;

import java.security.PublicKey;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.EmptyStackException;

import org.w3c.dom.Text;

import com.asm.DBloader.ASM_DBlistDS;
import com.asm.DBloader.ASM_FAreaDS;
import com.asm.DBloader.ASM_FCompanyBranch;
import com.asm.DBloader.ASM_FCompanyBranchDS;
import com.asm.DBloader.ASM_FDaynPrdHed;
import com.asm.DBloader.ASM_FDaynPrdHedDS;
import com.asm.DBloader.ASM_Fdebtor;
import com.asm.DBloader.ASM_FdebtorDS;
import com.asm.DBloader.ASM_Serverinfo;
import com.asm.DBloader.ASM_ServerinfoDS;
import com.asm.DBloader.ASM_Usersettings;
import com.asm.DBloader.ASM_UsersettingsDS;
import com.asm.DBloader.ASM_fCompanySetting;
import com.asm.DBloader.ASM_fCompanySettingDS;
import com.asm.DBloader.ASM_fDaynPrdDet;
import com.asm.DBloader.ASM_fDaynPrdDetDS;
import com.asm.DBloader.ASM_fReasonDS;
import com.dspl.asmswd.R;

import android.R.integer;
import android.R.string;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.webkit.WebView.FindListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.app.ListActivity;
import android.content.DialogInterface;


public class ASM_Pcals_fragment extends Fragment 
{
	
	public ASM_Pcals_fragment(){}
	private ViewGroup mContainerView;
	String getSelectedReson = null;
	String getDebtorname = null;
	String debtorname = null;
	String ASMCode =null;
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) 
	{
		
        View rootView = inflater.inflate(R.layout.activity_layout_changes, container, false);
        
        setHasOptionsMenu(true); 
        
        
        mContainerView = (ViewGroup)rootView.findViewById(R.id.container);
        loadhistory();
        return rootView;
    }
	  
    private void addItem(String txtrefno,String getSelectedReson) 
    {
        // Instantiate a new "row" view.
        final ViewGroup newView = (ViewGroup) LayoutInflater.from(getActivity()).inflate(
                R.layout.list_item_example, mContainerView, false);
        
        
        ((TextView) newView.findViewById(android.R.id.text1)).setText("");
        // Set the text in the new row to a random country.
       ((TextView) newView.findViewById(android.R.id.text1)).setText(txtrefno.toString()+" -:- "+getSelectedReson.toString());

        // Set a click listener for the "X" button in the row that will remove the row.
        newView.findViewById(R.id.delete_button).setOnClickListener(new View.OnClickListener() 
        {
            @Override
            public void onClick(View view) 
            {
        		AlertDialog.Builder builder1 = new AlertDialog.Builder(getActivity());
        		builder1.setTitle("ATTENTION..!");
    			builder1.setIcon(R.drawable.ic_attention);
	            builder1.setMessage("Are You Sure Want To Delete This Record...This Step Can't Be reverse...");
	            builder1.setCancelable(true);
	            builder1.setPositiveButton("OK",
	            new DialogInterface.OnClickListener()
	            {
	                public void onClick(DialogInterface dg, int id) 
	                {
	                	   mContainerView.removeView(newView);
	                	   String refno  = ((TextView) newView.findViewById(android.R.id.text1)).getText().toString().substring(0, 12);
	                	   ASM_FDaynPrdHedDS ds2=new ASM_FDaynPrdHedDS(getActivity());
	                	   ASM_fDaynPrdDetDS ds3=new ASM_fDaynPrdDetDS(getActivity());
	                	   ds2.Delete(refno);
	                	   ds3.Delete(refno);

	                       if (mContainerView.getChildCount() == 0) 
	                       {
	                           
	                       }
	                	dg.dismiss();
	           
	                }
	            });
	            
	            builder1.setNegativeButton("NO",  
	            new DialogInterface.OnClickListener()
	            {
	                public void onClick(DialogInterface dg, int id) 
	                {
	                dg.dismiss();
	                }
	            });
	            AlertDialog alert11 = builder1.create();
	            alert11.show();
                // Remove the row from its parent (the container view).
                // Because mContainerView has android:animateLayoutChanges set to true,
                // this removal is automatically animated.
             
            }
        });
        newView.findViewById(R.id.add_buttton).setOnClickListener(new View.OnClickListener() 
        {
			
			@Override
			public void onClick(View v) 
			{
				// TODO Auto-generated method stub
			
				String refno  = ((TextView) newView.findViewById(android.R.id.text1)).getText().toString().substring(0, 12);
				addPcals(refno);
				//mContainerView.refreshDrawableState();
				mContainerView.removeView(newView);
	
				
			}
		});
        
        newView.findViewById(R.id.view_button).setOnClickListener(new View.OnClickListener() 
        {
			
    			@Override
    			public void onClick(View v) 
    			{

    				String refno  = ((TextView) newView.findViewById(android.R.id.text1)).getText().toString().substring(0, 12);
    				viewrecord(refno);
    				// TODO Auto-generated method stub
    			
    			}
    		});
        

        // Because mContainerView has android:animateLayoutChanges set to true,
        // adding this view is automatically animated.
        mContainerView.addView(newView, 0);
    }
	
	
	public void addPcals(String refno) 
	{
		final Dialog dialog = new Dialog(getActivity());
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.asm_dialogbox_productivecal);
		dialog.setCancelable(false);
		
		Button btncancel  = (Button)dialog.findViewById(R.id.btncancel);
		Button btnclear  = (Button)dialog.findViewById(R.id.btnclear);
		Button btnadd  = (Button)dialog.findViewById(R.id.btngen);
		final Spinner spin2 = (Spinner)dialog.findViewById(R.id.spin2);
		final EditText Remark = (EditText)dialog.findViewById(R.id._remarks);
		
		final TextView txtdate = (TextView)dialog.findViewById(R.id.txtdate);
		final AutoCompleteTextView spin3 = (AutoCompleteTextView)dialog.findViewById(R.id.spin3);
		final EditText txtrefno = (EditText)dialog.findViewById(R.id.txtrefno);
		CheckBox checkBox1 = (CheckBox)dialog.findViewById(R.id.checkBox1);
		CheckBox checkBox2 = (CheckBox)dialog.findViewById(R.id.checkBox2);
		final AutoCompleteTextView Teratory = (AutoCompleteTextView)dialog.findViewById(R.id._searchTerotory);
		
		
		txtdate.setText(getCurrentTimeString());
		
		 if(refno!="no")
		 {
			 
			 
			final String RefNo1 = refno;
			final String Date;
			
			ArrayList<String> reson = new ArrayList<String>();
				
			ASM_fDaynPrdDetDS ds2=new ASM_fDaynPrdDetDS(getActivity());
			ArrayList<String> list3 =ds2.getfDaynPrdDet(RefNo1);
			ArrayAdapter<String> categoriesAdapter3 = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.item, list3);
		
			ASM_FDaynPrdHedDS ds3=new ASM_FDaynPrdHedDS(getActivity());
			ArrayList<String> list1 =ds3.getfDaynPrdHed(RefNo1);
			ArrayAdapter<String> categoriesAdapter1 = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.item, list1);

			reson.add(categoriesAdapter3.getItem(3).toString());
			
			ArrayAdapter<String> categoriesAdapter4 = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.item, reson);
			
			spin2.setAdapter(categoriesAdapter4);
			
			
			
			txtrefno.setText(categoriesAdapter3.getItem(0).toString()); 
			txtdate.setText(categoriesAdapter1.getItem(1).toString());
			//PNcalls.setText(categoriesAdapter.getItem(4).toString());
			spin3.setText(categoriesAdapter3.getItem(2).toString());
			//Teratory.setText(categoriesAdapter3.getItem(2).toString());	

			spin2.setSelection(categoriesAdapter3.getPosition(RefNo1));
			Date = categoriesAdapter1.getItem(1).toString();
	
				txtrefno.setClickable(false);
				txtdate.setClickable(false);
			
				
				
				
				
				
				
				
				ASM_FdebtorDS ds=new ASM_FdebtorDS(getActivity());
				ArrayList<String> list =ds.getDebtors("");
				ArrayAdapter<String> categoriesAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.item, list);
				spin3.setAdapter(categoriesAdapter);
				 
				spin3.setOnItemSelectedListener(new OnItemSelectedListener() 
				{

					@Override
					public void onItemSelected(AdapterView<?> parent, View view,
							int position, long id) 
					{
						// TODO Auto-generated method stub
						getDebtorname = parent.getItemAtPosition(position).toString();
			
					}

					@Override
					public void onNothingSelected(AdapterView<?> parent) 
					{
						// TODO Auto-generated method stub
						
					}
				});
				
				ASM_fReasonDS ds1=new ASM_fReasonDS(getActivity());
				ArrayList<String> list2 =ds1.getReason();
				
				list2.add(0,categoriesAdapter3.getItem(3).toString());
				ArrayAdapter<String> categoriesAdapter2 = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.item, list2);
				spin2.setAdapter(categoriesAdapter2);
				spin2.setOnItemSelectedListener(new OnItemSelectedListener() 
				{

					@Override
					public void onItemSelected(AdapterView<?> parent, View view,
							int position, long id)
					{
						// TODO Auto-generated method stub
						 getSelectedReson  = parent.getItemAtPosition(position).toString();
					}

					@Override
					public void onNothingSelected(AdapterView<?> parent)
					{
						// TODO Auto-generated method stub
						
					}
				});
				
				btnadd.setOnClickListener(new View.OnClickListener()
				{
					
					@SuppressWarnings("unused")
					@Override
					public void onClick(View v)
					{
						
						// TODO Auto-generated method stub
						if(!spin2.getSelectedItem().toString().equals("SELECT REASON") && !spin3.getText().toString().equals(""))
						{

						try 
						{
						debtorname =  spin3.getText().toString();
						String kk = debtorname.toString().trim();
						String[] debtroname = debtorname.split("-:-");
						
							
						
						ASM_UsersettingsDS ds2=new ASM_UsersettingsDS(getActivity());
						ASMCode = ds2.getASMCode();
						
						ASM_FDaynPrdHed asDblist2 = new ASM_FDaynPrdHed();
						ASM_FDaynPrdHedDS DS3 = new ASM_FDaynPrdHedDS(getActivity());
						
						asDblist2.setAsmFnonHed_RefNo(RefNo1.toString());
						asDblist2.setAsmFnonHed_AddUser(ASMCode.toString());
						asDblist2.setAsmFnonHed_AddDate(Date.toString());
						asDblist2.setAsmFnonHed_TxnDate(Date.toString());
						asDblist2.setAsmFnonHed_CostCode("");
						asDblist2.setAsmFnonHed_DealCode("");
						asDblist2.setAsmFnonHed_Remarks(Remark.getText().toString());
						asDblist2.setAsmFnonHed_TranBatch("");
						asDblist2.setAsmFnonHed_RepCode("");
						asDblist2.setAsmFnonHed_AddMach("");
						asDblist2.setAsmFnonHed_TranBatch("");

						
						ASM_fDaynPrdDet asDblist3 = new ASM_fDaynPrdDet();
						ASM_fDaynPrdDetDS DS4 = new ASM_fDaynPrdDetDS(getActivity());
						asDblist3.setAsmFnonDet_RefNo(RefNo1.toString());
						asDblist3.setAsmFnonDet_TxnDate(Date.toString());
						asDblist3.setAsmFnonDet_DebCode(debtroname[0].toString().trim());
						asDblist3.setAsmFnonDet_Reason(getSelectedReson.toString());
						asDblist3.setAsmFnonDet_RecordId("");
						asDblist3.setAsmFnonDet_Longitude("");
						asDblist3.setAsmFnonDet_Latitude("");
						asDblist3.setAsmFnonDet_RepCode(ASMCode.toString());
						
						DS4.Update(asDblist3);
						DS3.Update(asDblist2);
						
						addItem(RefNo1,getSelectedReson);
							
						} 
						catch (Exception e)
						{
							Toast.makeText(getActivity(), RefNo1.toString()+"is not update sucessfully..contact your administrator",Toast.LENGTH_LONG).show();
							// TODO: handle exception
							Log.v("Error", e.toString());
						}

						dialog.dismiss();
						}
						else 
						{

							AlertDialog.Builder builder1 = new AlertDialog.Builder(getActivity());
							builder1.setTitle("ATTENTION..!");
			    			builder1.setIcon(R.drawable.ic_attention);
				            builder1.setMessage("Please Select Reason & Customer...");
				            builder1.setCancelable(true);
				            builder1.setPositiveButton("Ok",
				                new DialogInterface.OnClickListener()
				            {
				                public void onClick(DialogInterface dg, int id) 
				                {
				                	dg.dismiss();
				           
				                }
				            });
				            AlertDialog alert11 = builder1.create();
				            alert11.show();	
						}
						
					}
				});
				
				btncancel.setOnClickListener(new View.OnClickListener() 
				{
					@Override
					public void onClick(View v) 
					{
						// TODO Auto-generated method stub
						mContainerView.removeAllViews();
						dialog.dismiss();
						loadhistory();
					}
				});
				
				btnclear.setOnClickListener(new View.OnClickListener() 
				{
					
					@Override
					public void onClick(View v) 
					{
						// TODO Auto-generated method stub
						if(txtrefno != null || txtdate != null)
						{
							spin3.setText("");
						}
						
						
					}
				});
				
				dialog.show();
			////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
		 }
		 else 
		 {
			 final String cSettingcode = "ASMPRODUCT";
				String empty = "empty";
				
				
				
				final String Refno = refnogenrator(cSettingcode,empty);
				txtrefno.setClickable(false);
				txtdate.setClickable(false);
				txtrefno.setText(Refno);
				
				ASM_FAreaDS dsa=new ASM_FAreaDS(getActivity());
				ArrayList<String> list5 =dsa.getArea();
				ArrayAdapter<String> categoriesAdapter5 = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.item, list5);
				Teratory.setAdapter(categoriesAdapter5);
				
				Teratory.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						// TODO Auto-generated method stub
						
				
				ASM_FdebtorDS ds=new ASM_FdebtorDS(getActivity());
				ArrayList<String> list =ds.getDebtors(parent.getItemAtPosition(position).toString().split("-:-")[1].toString().trim());
				ArrayAdapter<String> categoriesAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.item, list);
				spin3.setAdapter(categoriesAdapter);
				
				spin3.setOnItemSelectedListener(new OnItemSelectedListener() 
				{
					@Override
					public void onItemSelected(AdapterView<?> parent, View view,
							int position, long id) 
					{
						// TODO Auto-generated method stub
						getDebtorname = parent.getItemAtPosition(position).toString();
					}
					@Override
					public void onNothingSelected(AdapterView<?> parent) 
					{
						// TODO Auto-generated method stub
						
					}
				});
				
					}
				});
				ASM_fReasonDS ds1=new ASM_fReasonDS(getActivity());
				ArrayList<String> list2 =ds1.getReason();
				ArrayAdapter<String> categoriesAdapter2 = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.item, list2);
				list2.add(0,"SELECT REASON");
				spin2.setAdapter(categoriesAdapter2);
				
				spin2.setOnItemSelectedListener(new OnItemSelectedListener() 
				{

					@Override
					public void onItemSelected(AdapterView<?> parent, View view,
							int position, long id)
					{
						// TODO Auto-generated method stub
						 getSelectedReson  = parent.getItemAtPosition(position).toString();
					}

					@Override
					public void onNothingSelected(AdapterView<?> parent)
					{
						// TODO Auto-generated method stub
						
					}
				});
				
				btnadd.setOnClickListener(new View.OnClickListener()
				{
					
					@SuppressWarnings("unused")
					@Override
					public void onClick(View v)
					{
						
						// TODO Auto-generated method stub
						if(!spin2.getSelectedItem().toString().equals("SELECT REASON") && !spin3.getText().toString().equals(""))
						{
						
						addItem(Refno,getSelectedReson);
						String seperate = Refno.substring(5);
						int incriment = Integer.parseInt(seperate);
						int newincriment = incriment + 1;
//						 
						try {
						debtorname =  spin3.getText().toString();
						String kk = debtorname.toString().trim();
						String[] debtroname = debtorname.split("-:-");
			
						ASM_FDaynPrdHed asDblist2 = new ASM_FDaynPrdHed();
						ASM_FDaynPrdHedDS DS3 = new ASM_FDaynPrdHedDS(getActivity());
						
						asDblist2.setAsmFnonHed_RefNo(Refno.toString());
						asDblist2.setAsmFnonHed_AddUser(ASMCode.toString());
						asDblist2.setAsmFnonHed_AddDate(getCurrentTimeString().toString());
						asDblist2.setAsmFnonHed_TxnDate(getCurrentTimeString().toString());
						asDblist2.setAsmFnonHed_CostCode("");
						asDblist2.setAsmFnonHed_DealCode("");
						asDblist2.setAsmFnonHed_Remarks(Remark.getText().toString());
						asDblist2.setAsmFnonHed_TranBatch("");
						asDblist2.setAsmFnonHed_RepCode("");
						asDblist2.setAsmFnonHed_AddMach("");
						asDblist2.setAsmFnonHed_TranBatch("");
						asDblist2.setAsmFnonHed_Issynced("0");
						GPSTracker gps = new GPSTracker(getActivity());
						 if(gps.canGetLocation())
						 {
			                    
							 double lati = gps.getLatitude();
			                 double longi = gps.getLongitude();
			                 asDblist2.setAsmFnonHed_Longitude(Double.toString(longi));
			                 asDblist2.setAsmFnonHed_Latitude(Double.toString(lati));
			                 Toast.makeText(getActivity(), "Your Location is - \nLat: " + lati + "\nLong: " + longi, Toast.LENGTH_LONG).show();   

						 }
						 else
						 {
			                   gps.showSettingsAlert();
			                   double lati = gps.getLatitude();
			                   double longi = gps.getLongitude();
			                   asDblist2.setAsmFnonHed_Longitude(Double.toString(longi));
				               asDblist2.setAsmFnonHed_Latitude(Double.toString(lati));
			                   Toast.makeText(getActivity(), "Your Location is - \nLat: "  , Toast.LENGTH_LONG).show();   
			               }
						
						
						
						
						ASM_fDaynPrdDet asDblist3 = new ASM_fDaynPrdDet();
						ASM_fDaynPrdDetDS DS4 = new ASM_fDaynPrdDetDS(getActivity());
						asDblist3.setAsmFnonDet_RefNo(Refno.toString());
						asDblist3.setAsmFnonDet_TxnDate(getCurrentTimeString().toString());
						asDblist3.setAsmFnonDet_DebCode(debtroname[0].toString());
						asDblist3.setAsmFnonDet_Reason(getSelectedReson.toString());
						asDblist3.setAsmFnonDet_RecordId("");
						asDblist3.setAsmFnonDet_Longitude("");
						asDblist3.setAsmFnonDet_Latitude("");
						asDblist3.setAsmFnonDet_RepCode(ASMCode.toString());
						DS4.Create(asDblist3);
						DS3.Create(asDblist2);
						
						
						} 
						catch (Exception e)
						{
							Toast.makeText(getActivity(), Refno.toString()+"is not save sucessfully..contact your administrator",Toast.LENGTH_LONG).show();
							// TODO: handle exception
						}
						
						ASM_FCompanyBranch asDblist1 = new ASM_FCompanyBranch();
						ASM_FCompanyBranchDS DS1 = new ASM_FCompanyBranchDS(getActivity());
						
						asDblist1.setAsmFCompanyBranch_cSettingsCode(cSettingcode);
						asDblist1.setAsmFCompanyBranch_nNumVal(Integer.toString(newincriment));
						DS1.Create(asDblist1);
						dialog.dismiss();
						}
						else 
						{

							AlertDialog.Builder builder1 = new AlertDialog.Builder(getActivity());
							builder1.setTitle("ATTENTION..!");
			    			builder1.setIcon(R.drawable.ic_attention);
				            builder1.setMessage("Please Select Reason & Customer...");
				            builder1.setCancelable(true);
				            builder1.setPositiveButton("Ok",
				                new DialogInterface.OnClickListener()
				            {
				                public void onClick(DialogInterface dg, int id) 
				                {
				                	dg.dismiss();
				           
				                }
				            });
				            AlertDialog alert11 = builder1.create();
				            alert11.show();	
						}
						
					}
				});
				
				btncancel.setOnClickListener(new View.OnClickListener() 
				{
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						
						dialog.dismiss();
					}
				});
				
				btnclear.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						if(txtrefno != null || txtdate != null)
						{
												
							spin3.setText("");
							
						}
							
					}
				});
				
				dialog.show();
		}
	}
		
	@Override
	public boolean onOptionsItemSelected(MenuItem item) 
	{

		switch (item.getItemId()) 
		{
		case R.id.action_add_item:
			addPcals("no");
			
			return true;
				default:
			return super.onOptionsItemSelected(item);
		}
	}
	
	public void graphview() {
	  	FragmentTransaction transaction = getFragmentManager().beginTransaction();
			transaction.replace(R.id.frame_container, new PieChartActivity());
			transaction.addToBackStack(null);
			transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
			transaction.commit();
	}
	
	
	public String refnogenrator(String cSettingsCode,String ASMPreFix) 
	{
		ASM_UsersettingsDS ds2=new ASM_UsersettingsDS(getActivity());
		ASMCode = ds2.getASMCode(); 
		
		ASM_fCompanySettingDS ds1=new ASM_fCompanySettingDS(getActivity());
		String refcode =ds1.getFcompanyDetails(cSettingsCode,ASMCode);
		
		ASM_FCompanyBranchDS ds=new ASM_FCompanyBranchDS(getActivity());
		String tempRepExpenseSerialStr = ds.getFcompanybranch(cSettingsCode);
		if(tempRepExpenseSerialStr==null)
		{
			tempRepExpenseSerialStr = "1";
		}
		
		String currentExpenseRefCode = null;
		
		

		try {
			if (tempRepExpenseSerialStr.length() == 0) 
			{
				String newInvNumberStr = "0000000" + tempRepExpenseSerialStr;
				currentExpenseRefCode = refcode.trim() + "/"+ newInvNumberStr;
			} 
			else if (tempRepExpenseSerialStr.length() == 1)
			{
				String newInvNumberStr = "000000" + tempRepExpenseSerialStr;
				currentExpenseRefCode = refcode.trim() + "/"+ newInvNumberStr;
			} 
			else if (tempRepExpenseSerialStr.length() == 2) 
			{
				String newInvNumberStr = "00000" + tempRepExpenseSerialStr;
				currentExpenseRefCode = refcode.trim() + "/"+ newInvNumberStr;
			}
			else if (tempRepExpenseSerialStr.length() == 3) 
			{
				String newInvNumberStr = "0000" + tempRepExpenseSerialStr;
				currentExpenseRefCode = refcode.trim() + "/"+ newInvNumberStr;
			} 
			else if (tempRepExpenseSerialStr.length() == 4) 
			{
				String newInvNumberStr = "000" + tempRepExpenseSerialStr;
				currentExpenseRefCode = refcode.trim() + "/"+ newInvNumberStr;
			} 
			else if (tempRepExpenseSerialStr.length() == 5) 
			{
				String newInvNumberStr = "00" + tempRepExpenseSerialStr;
				currentExpenseRefCode = refcode.trim() + "/"+ newInvNumberStr;
			} 
			else if (tempRepExpenseSerialStr.length() == 5) 
			{
				String newInvNumberStr = "0" + tempRepExpenseSerialStr;
				currentExpenseRefCode = refcode.trim() + "/"+ newInvNumberStr;
			} 
			else 
			{
				String newInvNumberStr = tempRepExpenseSerialStr;
				currentExpenseRefCode = refcode.trim() + "/"+ newInvNumberStr;
			}
		} catch (Exception e) {

		}

		
		
		return currentExpenseRefCode;
	}
	
	public void viewrecord(String Refno) 
	{
		// TODO Auto-generated method stub
	
		final Dialog dialog = new Dialog(getActivity());
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.asm_dialog_productiveview);
		dialog.setCancelable(false);
		
		TextView TextRefNo = (TextView)dialog.findViewById(R.id.TextRefNo);
		TextView TextDate = (TextView)dialog.findViewById(R.id.TextDate);
		TextView PNcalls = (TextView)dialog.findViewById(R.id.PNcalls);
		TextView txtcustomer = (TextView)dialog.findViewById(R.id.txtcustomer);
		TextView Txtreson = (TextView)dialog.findViewById(R.id.Txtreson);
		TextView Remarks = (TextView)dialog.findViewById(R.id._remarksq);
	
		

		
		ASM_fDaynPrdDetDS ds2=new ASM_fDaynPrdDetDS(getActivity());
		ArrayList<String> list =ds2.getfDaynPrdDet(Refno);
		ArrayAdapter<String> categoriesAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.item, list);
	
		ASM_FDaynPrdHedDS ds3=new ASM_FDaynPrdHedDS(getActivity());
		ArrayList<String> list1 =ds3.getfDaynPrdHed(Refno);
		ArrayAdapter<String> categoriesAdapter1 = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.item, list1);
	
		
		
		TextRefNo.setText(categoriesAdapter.getItem(0).toString()); 
		TextDate.setText(categoriesAdapter1.getItem(1).toString());
		PNcalls.setText(categoriesAdapter.getItem(4).toString());
		txtcustomer.setText(categoriesAdapter.getItem(2).toString());
		Txtreson.setText(categoriesAdapter.getItem(3).toString());
		Remarks.setText(categoriesAdapter1.getItem(4).toString());
	

		Button btnOK = (Button)dialog.findViewById(R.id.btnOK);	
		btnOK.setOnClickListener(new View.OnClickListener() 
		{
			
			@Override
			public void onClick(View v)
			{
				dialog.dismiss();
			}
		});
				
		dialog.show();
		
	}
	
	public void loadhistory() 
	{

		ASM_FDaynPrdHedDS ds3=new ASM_FDaynPrdHedDS(getActivity());
		ArrayList<String> list1 =ds3.getfDaynPrdHedall();
		ArrayAdapter<String> categoriesAdapter1 = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.item, list1);
		
		ASM_fDaynPrdDetDS ds2=new ASM_fDaynPrdDetDS(getActivity());
		ArrayList<String> list =ds2.getfDaynPrdDetall();
		ArrayAdapter<String> categoriesAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.item, list);
		
		
		for (int i = 0; i < categoriesAdapter1.getCount(); i++) 
		{
			addItem(categoriesAdapter1.getItem(i).toString(),categoriesAdapter.getItem(i).toString());
		}

		
	}
	private String getCurrentTimeString() 
	{
		// 2013-10-04 00:00:00.000
		Date dateNow = new Date();
		Calendar c = Calendar.getInstance();
		SimpleDateFormat ddMMMyyFormat = new SimpleDateFormat("yyyy/MM/dd");
		String date_to_string = ddMMMyyFormat.format(dateNow);
		String cusDate = date_to_string;
		return cusDate;
	}
	 public boolean onKeyDown(int keyCode, KeyEvent event) {
	     if(keyCode==KeyEvent.KEYCODE_BACK)
	         Toast.makeText(getActivity(), "Use Home Button",      
	      Toast.LENGTH_LONG).show();

	     return false;
	        // Disable back button..............
	 }
	 
}
